<template>
  <a-drawer
    :title="title"
    width="40%"
    :visible="visible"
    :confirmLoading="loading"
    @close="
      () => {
        this.visible = false
      }
    "
  >
    <a-spin :spinning="loading">
      <a-form-model ref="form" :model="entity" :rules="rules" v-bind="layout">
        <a-form-model-item label="主键" prop="Id" v-show="false">
          <a-input v-model="entity.Id" autocomplete="off" placeholder="主键" />
        </a-form-model-item>
        <a-form-model-item label="税务编码" prop="TM_TaxCode">
          <a-input v-model="entity.TM_TaxCode" autocomplete="off" placeholder="税务编码" />
        </a-form-model-item>
        <a-form-model-item label="物料简码" prop="TM_AbbCode">
          <a-input v-model="entity.TM_AbbCode" autocomplete="off" placeholder="物料简码" />
        </a-form-model-item>
        <a-form-model-item label="物料编码" prop="TM_Code">
          <a-input v-model="entity.TM_Code" autocomplete="off" placeholder="物料编码" />
        </a-form-model-item>
        <a-form-model-item label="物料名称" prop="TM_Name">
          <a-input v-model="entity.TM_Name" autocomplete="off" placeholder="物料名称" />
        </a-form-model-item>
        <a-form-model-item label="物料规格" prop="TM_Specs">
          <a-input v-model="entity.TM_Specs" autocomplete="off" placeholder="物料规格" />
        </a-form-model-item>
        <a-form-model-item label="计量单位" prop="TM_Unit">
          <a-input v-model="entity.TM_Unit" autocomplete="off" placeholder="计量单位" />
        </a-form-model-item>
        <a-form-model-item label="物料单价" prop="TM_UnitPrice">
          <a-input v-model="entity.TM_UnitPrice" autocomplete="off" placeholder="物料单价" />
        </a-form-model-item>
        <a-form-model-item label="是否含税" prop="TM_IsTax">
          <a-switch v-model="entity.TM_IsTax" :checked="entity.TM_IsTax" />
        </a-form-model-item>
        <a-form-model-item label="税率" prop="TM_TaxRate">
          <a-input v-model="entity.TM_TaxRate" autocomplete="off" placeholder="税率" />
        </a-form-model-item>        
      </a-form-model>
    </a-spin>
    <div
      :style="{
        position: 'absolute',
        right: 0,
        bottom: 0,
        width: '100%',
        borderTop: '1px solid #e9e9e9',
        padding: '4px 16px',
        background: '#fff',
        textAlign: 'right',
        zIndex: 1
      }"
    >
      <a-button type="primary" @click="handleSave">确定</a-button>&nbsp;
      <a-button @click="close">取消</a-button>&nbsp;
    </div>
  </a-drawer>
</template>

<script>
export default {
  props: {
    parentObj: Object
  },
  data () {
    return {
      layout: {
        labelCol: { span: 5 },
        wrapperCol: { span: 18 }
      },
      visible: false,
      loading: false,
      entity: {},
      rules: {
        TM_TaxCode: [
          { required: true, message: '请填写税务编码', trigger: 'blur' },
        ],
        TM_AbbCode: [
          { required: true, message: '请填写物料简码', trigger: 'blur' },
        ],
        TM_Code: [
          { required: true, message: '请填写物料编码', trigger: 'blur' },
        ],
        TM_Name: [
          { required: true, message: '请填写物料名称', trigger: 'blur' },
        ],
        TM_TaxRate: [
          { required: true, message: '请填写税率', trigger: 'blur' },
        ],
        
      },
      title: ''
    }
  },
  methods: {
    handleSave(){
      this.$refs.form.validate(valid => {
        if (valid) {
         if (this.entity.TM_IsTax) { this.entity.TM_IsTax = 1 } else { this.entity.TM_IsTax = 0 }
          this.$http.post('/MaterialManagement/TaxMaterials/SaveData', this.entity).then(res => {
            if (res.Success) {
              this.$message.success('保存成功!')
              this.visible = false
              this.parentObj.getDataList();
            }
          }).catch(err => {
            this.$message.error(err)
          })
        } else {
          console.log('error submit!!');
          return false;
        }})
      
    },
    close() {
      this.visible = false
      this.entity = {}
    },
    init () {
      this.visible = true
      this.entity = {}
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    },
    openForm (id, title) {
      this.init()
      this.title = title
      if (id) {
        this.loading = true
        this.$http.post('/MaterialManagement/TaxMaterials/GetTheData', { id: id }).then(resJson => {
          this.loading = false

          this.entity = resJson.Data
        })
      }
    },
    handleSubmit () {
      this.$refs['form'].validate(valid => {
        if (!valid) {
          return
        }
        this.loading = true
        this.$http.post('/OrderManagement/Ap_CloseBill/SaveData', this.entity).then(resJson => {
          this.loading = false

          if (resJson.Success) {
            this.$message.success('操作成功!')
            this.visible = false

            this.parentObj.getDataList()
          } else {
            this.$message.error(resJson.Msg)
          }
        })
      })
    }
  }
}
</script>
